package grape.base.rest.message.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.service.message.query.MessageTemplateListQuery;
import grape.common.exception.runtime.RBaseException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.base.rest.message.form.MessageTemplateCreateForm;
import grape.base.rest.message.form.MessageTemplateUpdateForm;
import grape.base.rest.message.form.MessageTemplateListPageForm;
import grape.base.rest.message.vo.MessageTemplateVo;
import grape.base.rest.message.mapper.MessageTemplateWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.base.service.message.po.MessageTemplate;
import grape.base.service.message.api.IMessageTemplateService;
import java.util.List;
/**
 * <p>
 * 消息内容模板表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-03-18
 */
@RestController
@RequestMapping("/base/messagetemplate")
@Api(tags = "消息内容模板相关接口")
public class MessageTemplateController extends BaseController<MessageTemplateVo, MessageTemplate> {

    @Autowired
    private MessageTemplateWebMapper currentWebMapper;
    @Autowired
    private IMessageTemplateService currentService;



     @ApiOperation("添加消息内容模板")
     @PreAuthorize("hasAuthority('messagetemplate:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public MessageTemplateVo create(@RequestBody @Valid MessageTemplateCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         MessageTemplate po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询消息内容模板")
     @PreAuthorize("hasAuthority('messagetemplate:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public MessageTemplateVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除消息内容模板")
     @PreAuthorize("hasAuthority('messagetemplate:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新消息内容模板")
     @PreAuthorize("hasAuthority('messagetemplate:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public MessageTemplateVo update(@PathVariable String id,@RequestBody @Valid MessageTemplateUpdateForm uf) {
         MessageTemplate po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询消息内容模板")
    @PreAuthorize("hasAuthority('messagetemplate:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<MessageTemplateVo> listPage(MessageTemplateListPageForm listPageForm) {
         MessageTemplate po = currentWebMapper.formToPo(listPageForm);
        MessageTemplateListQuery messageTemplateListQuery = new MessageTemplateListQuery();
        messageTemplateListQuery.setPoQuery(po);
        return super.listPage(messageTemplateListQuery,listPageForm);
     }

}
